<template>
  <section class="search">
    <search-header title="搜索"/>
    <form class="search_form" @submit.prevent="search">
      <input type="search" placeholder="请输入商家名称" class="search_input" v-model="keyword">
      <input type="submit" class="search_submit">
    </form>
    <section class="list" v-if="!noSearchShops">
      <ul class="list_container">
        <router-link
          :to="{path:'/shop', query:{id:item.id}}"
          tag="li"
          v-for="item in searchShops"
          :key="item.id"
          class="list_li"
        >
          <section class="item_left">
            <img :src="imgBaseUrl + item.image_path" class="restaurant_img">
          </section>
          <section class="item_right">
            <div class="item_right_text">
              <p>
                <span>{{item.name}}</span>
              </p>
              <p>月售 {{item.month_sales||item.recent_order_num}} 单</p>
              <p>{{item.delivery_fee||item.float_minimum_order_amount}} 元起送 / 距离
                {{item.distance}}</p>
            </div>
          </section>
        </router-link>
      </ul>
    </section>
    <div class="search_none" v-if="noSearchShops">很抱歉！无搜索结果</div>
  </section>
</template>

<script>
import SearchHeader from '../../components/HeaderTop/HeaderTop.vue'
import {mapState} from 'vuex'
export default {
  name: 'Search',
  components: {
    SearchHeader
  },
  data () {
    return {
      keyword: '',
      imgBaseUrl: 'http://cangdu.org:8001/img/',
      noSearchShops: false
    }
  },
  computed: {
    ...mapState(['searchShops'])
  },
  methods: {
    search () {
      // 得到搜索关键字
      const keyword = this.keyword.trim() // 去除前后空格trim
      // 进行搜索
      if (keyword) {
        this.$store.dispatch('searchShops', keyword) // 要传一个参数keyword给vuex
      }
    }
  },
  watch: {
    searchShops (val) {
      if (!val.length) { // 没有数据
        this.noSearchShops = true
      } else { // 有数据
        this.noSearchShops = false
      }
    }
  }
}
</script>

<style lang="stylus" rel="stylesheet/stylus" scoped>
  @import "../../common/stylus/mixins.styl"
  .search
    width 100%
    height 100%
    overflow hidden
    background white
    .search_form
      clearFix()
      margin-top 90px
      background-color #fff
      padding 24px 16px
      input
        height 70px
        padding 0 8px
        border-radius 4px
        font-weight bold
        outline none
        &.search_input
          float left
          width 79%
          border 8px solid #f2f2f2
          font-size 26.8px
          color #333
          background-color #f2f2f2
        &.search_submit
          float right
          width 18%
          border 8px solid #02a774
          font-size 32px
          color #fff
          background-color #02a774

    .list
      .list_container
        background-color: #fff;
        .list_li
          display: flex;
          justify-content: center;
          padding: 20px
          border-bottom: 2px solid $bc;
          .item_left
            margin-right: 20px
            .restaurant_img
              width 100px
              height 100px
              display block
          .item_right
            font-size 24px
            flex 1
            .item_right_text
              p
                margin-bottom 12px
                &:last-child
                  margin-bottom 0
    .search_none
      margin: 0 auto
      color: #333
      background-color: #fff
      text-align: center
      margin-top: 0.25rem
</style>
